<?php
namespace app\index\controller;

use app\common\model\LunwenAlipayOrderModel;
use app\common\service\OrderService;
use app\index\model\ZhiniaoUserModel;
use app\wechat\service\WechatService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;

/**
 * Class Index
 * @package app\index\controller
 */
class User extends Controller
{
    /**
     * 通用返回成功方法
     * @return mixed
     */
    public function returnSuccess($data, $message = 'success', $code = 200){
        return json(['code' => $code, 'msg' => $message, 'data' => $data]);
    }

    public function returnError($data, $message = 'success', $code = 500){
        return json(['code' => $code, 'msg' => $message, 'data' => $data]);
    }

    public function index(){
        $this->url = $this->request->url(true);
        $user = WechatService::getWebOauthInfo($this->url);
        $user = ZhiniaoUserModel::where(['openid' => $user['openid']])->find();
        if(!$user){
            return $this->fetch('error');
        }
        session('zhiniao_user', $user);
        $configJson = json_encode(WechatService::getWebJssdkSign(), JSON_UNESCAPED_UNICODE);
        $this->configJson = $configJson;
        return $this->fetch();
    }


    public function test(){

    }


    public function createUserOrder(){
        $user = session('zhiniao_user');
        $username = input('username', '', 'string');
        $user_exit = ZhiniaoUserModel::where(['username' => $username])->find();
        if($user_exit){
            return $this->returnError([], '该用户已存在，请更换用户名重试。');
        }
        $pay_amount = 0.01;
        $extra_data = [
            'username' => $username,
            'pid' => $user['id'],
            'password' => CodeExtend::random(6)
        ];
        $order_info = OrderService::createOrder($user, "开户：$username", OrderService::PAY_ORDER_CHANNEL_WX, $pay_amount, [
            'extra_data' => json_encode($extra_data)
        ]);
        $this->pay = WechatService::WePayOrder();
        $result = $this->pay->create([
            'body'             => $order_info['subject'],
            'openid'           => $user['openid'],
            'total_fee'        => $pay_amount * 100,
            'trade_type'       => 'JSAPI',
            'notify_url'       => 'http://ai.tinyask.cn/api/Charge/createUserNotify',
            'out_trade_no'     => $order_info['out_trade_no'],
            'spbill_create_ip' => $this->request->ip(),
        ]);
        $optionJson = json_encode($this->pay->jsapiParams($result['prepay_id']), JSON_UNESCAPED_UNICODE);
        return $this->returnSuccess([
            'optionJson' => $optionJson,
            'out_trade_no' => $order_info['out_trade_no']
        ]);
    }


    public function showOrder(){
        $out_trade_no = input('out_trade_no', '', 'string');
        if(!$out_trade_no){
            $this->message = '订单号有误';
            return $this->fetch('error');
        }
        $order = LunwenAlipayOrderModel::where(['out_trade_no' => $out_trade_no])->find();
        if(!$order){
            $this->message = '订单不存在';
            return $this->fetch('error');
        }
        if($order['trade_status'] != 'SUCCESS'){
            $this->message = '订单未成功支付';
            return $this->fetch('error');
        }

        $extra_data = json_decode($order['extra_data'], true);
        $this->extra_data = $extra_data;
        return $this->fetch('showorder');
    }
}